package dolf.objects.weapons.DriveByMG;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.ImageIcon;


import dolf.game.*;
import dolf.objects.Bullet;
import dolf.objects.ImageStore;
import dolf.objects.weapons.*;
import dolf.window.SystemMessagesHandler;

public class DriveByMG implements Weapon, java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 170;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.DriveByMG.lang.lang").getString("name");;
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.DriveByMG.lang.lang").getString("type");;
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.DriveByMG.lang.lang").getString("description");;
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/DriveByMG/gfx/icon.png");
	
	private int bullets;
	private final int MAX_BULLETS = 40;
	private Player pl;
	private boolean inititalized = false;
	private transient Cursor crosshair;
	

	public DriveByMG(){
		Image c = ImageStore.getInstance().getImage("dolf/gfx/environment/crosshair.png");
		crosshair = Toolkit.getDefaultToolkit().createCustomCursor(c,new Point(10,10), "Cursor" );		
	}
	
	
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		this.bullets = this.MAX_BULLETS;
		this.inititalized = true;
		this.pl = _pl;
		this.pl.setWeaponState();
	}
	public void deInitialize(){
		this.inititalized = false;
		this.pl = null;
		dolf.window.Canvas.getInstance().setCursor(null);
	}
	
	public void execute() {
		if (pl.getBall().isMoving() && bullets > 0) {
			if (crosshair == null) {
				Image c = ImageStore.getInstance().getImage("dolf/gfx/environment/crosshair.png");
				crosshair = Toolkit.getDefaultToolkit().createCustomCursor(c,new Point(10,10), "Cursor" );
			}
			dolf.window.Canvas.getInstance().setCursor(crosshair);
		}
		else dolf.window.Canvas.getInstance().setCursor(null);
		
		if(Level.getInstance().mousePressed()&&pl.getBall().isMoving()){
			if(this.bullets > 0){
				Level.getInstance().registerObjectOnTop(
						new Bullet(pl.getBall().getPosition().sub(pl.getBall().getPosition().sub(Level.getInstance().getMouse()).setLength(pl.getBall().getRadius()+pl.getBall().getMotion().getLength()+1)),
								Level.getInstance().getMouse().sub(pl.getBall().getPosition()).setLength(7),this.pl,1));
				this.bullets--;
				this.pl.setWeaponState();
				return;
			} 
		}	
	}

	public ImageIcon getIcon() {
		return Icon;
	}

	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}

	public int getPrice() {
		return Price;
	}
	public String getType(){
		return WeaponType;
	}

	public Player getPlayer(){
		return this.pl;
	}
	
	public void remove(){
		dolf.window.Canvas.getInstance().setCursor(null);
		pl.removeWeapon(this);
		this.pl.setWeaponState();
		this.pl.setAktWeapon(null);
	}
	public double getState(){
		return (double)this.bullets/this.MAX_BULLETS;
	}
}
